package com.ziyue.socket.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ziyue.socket.bean.Member;
import com.ziyue.socket.bean.activity.Activity;
import com.ziyue.socket.bean.answer.AnswerRightResponse;
import com.ziyue.socket.bean.log.ActionLog;
import com.ziyue.socket.bean.register.RegisterRequest;
import com.ziyue.socket.bean.Room;
import com.ziyue.socket.bean.register.RegisterResponse;
import com.ziyue.socket.bean.SocketResponse;
import com.ziyue.socket.cache.ActionLogService;
import com.ziyue.socket.enums.OptionEnum;
import com.ziyue.socket.enums.RoleEnum;
import com.ziyue.socket.enums.SocketStatusEnum;
import com.ziyue.socket.exception.UnFullWebsocketMessageException;
import com.ziyue.socket.service.OptionAbstract;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.*;

/**
 * Created by wancheng on 2019/4/18.
 */
public class Register extends OptionAbstract {

    private final OptionEnum OPTION = OptionEnum.REGISTER;

    @Override
    public Object send(Channel channel, String requestBody) throws UnFullWebsocketMessageException {

        RegisterRequest registerRequest = JSONObject.parseObject(requestBody,RegisterRequest.class);
        if (registerRequest.getLessonId() == null){
            throw  new UnFullWebsocketMessageException("无房间id");
        }
        if (registerRequest.getRole() == null){
            throw  new UnFullWebsocketMessageException("无身份信息");
        }
        if (registerRequest.getAccount() == null){
            throw  new UnFullWebsocketMessageException("无手机号码");
        }
        if (registerRequest.getLessonId() == null || registerRequest.getRole() == null){
            throw new UnFullWebsocketMessageException();
        }
        Room room = initRoom(channel,registerRequest);
        RegisterResponse registerResponse = new RegisterResponse();
        registerResponse.setActivityId(room.getCurrentActivityId());
        ActionLog inactiveLog = ActionLogService.getInactiveLog(channel);
        //if (inactiveLog == null){
            sendMsg(channel,"注册成功",registerResponse,OPTION);
        //}else{
        //    sendReActiveMsg(channel);
        //}
        return null;
    }

    /**
     * 初始化房间具体的一些参数
     * @param channel
     * @param registerRequest
     * @return
     */
    private Room initRoom(Channel channel,RegisterRequest registerRequest){
        Member member = new Member(registerRequest.getLessonId(),channel.id(), registerRequest.getRole(), registerRequest.getName(), registerRequest.getAccount());
        Room room = roomManage.initRoom(registerRequest.getLessonId());
        room.setCurrentActivityId(registerRequest.getActivityId());
        roomManage.addMember( member);
        return room;
    }
    private void sendReActiveMsg(Channel channel){
        Room room = roomManage.getRoom(channel);
        ActionLog inactiveLog = ActionLogService.getInactiveLog(channel);
        Member member = roomManage.getMember(channel.id());
        Activity activity = room.getActivities().get(inactiveLog.getActivityId());
        if (RoleEnum.TEACHER.equals(member.getRole())){
            //重置作答时间的限制
            Long useTime = inactiveLog.getActionTime().getTime() - activity.getStartTime().getTime();
            Long unUsed = activity.getLimit() * 1000 - useTime;
            Date endTime = new Date(System.currentTimeMillis() + unUsed);
            activity.setEndTime(endTime);

            Map<String, Object> res = new HashMap<>(4);
            Map<String, Map<String, List<String>>> left = getLeft(room);
            List<AnswerRightResponse> right = getRight(room);
            res.put("left",left);
            res.put("right",right);
            res.put("unUsedTime",unUsed / 1000);
            //老师发送全部学生作答情况
            this.sendMsg(channel,"学生作答情况",res,OPTION);
        }else {
            Long unUsed = activity.getEndTime().getTime() - System.currentTimeMillis();
            List<AnswerRightResponse> right = getRight(room);
            Optional<AnswerRightResponse> memberAnswerOptional = right.stream()
                    .filter(a->a.getAccount().equals(member.getAccount()))
                    .findFirst();
            AnswerRightResponse memberAnswer = memberAnswerOptional.get();
            Map<String, Object> res = new HashMap<>(4);
            res.put("answer",memberAnswer);
            res.put("unUsedTime",unUsed / 1000);
            this.sendMsg(channel,"学生作答情况",res,OPTION);
        }
    }

    @Override
    public Object get(Channel channel, String requestBody) {
        return null;
    }
}
